home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue35 / etimer / ETIMER.ZIP / ETIMER.PAS
Pascal/Delphi Source File  |  1998-06-09  |  5KB  |  156 lines

  1. unit ETimer;
  2. (*
  3.  Enhanced Timer V1.00 - released June 09, 1998
  4.  Copyright (c) 1998 by Demian
  5.  
  6.  Disclaimer:
  7.  This component is distributed as Freeware. There is no charge or warranty
  8.  whatsoever. The author will not be held responsible for any direct or indirect
  9.  damage resulting from the use of the component or any derivatives thereof.
  10.  In short: USE IT AT YOUR OWN RISK! You are allowed to include the component
  11.  code in any commercial or non commercial product. You are also allowed to make
  12.  as many copies of the component as you want and distribute them, as long as you
  13.  do not receive any money for the copying/distribution.
  14.  
  15.  You can send any comments, suggestions and bugs to demian@bhnet.com.br
  16.  New versions can be found at: http://www.bhnet.com.br/~demian
  17.                                http://web.unix.horizontes.com.br/~demian
  18.  
  19.  If you do any modification to this code, please send me a copy.
  20.  If you use this code on any 'real-life' application, please let me know.
  21.  If you think this code is completely useless, please DON'T LET ME KNOW!
  22.  
  23.  What it does:
  24.  TTimer descendent with a new property, Snooze, that keeps a counter of
  25.  miliseconds elapsed since the last time the user moved/clicked the mouse or
  26.  pressed a key. Useful to implement timeout routines.
  27.  
  28.  How to use it:
  29.  1. Install the component [VNM Pallete];
  30.  2. Drop one or more TETimer onto a form;
  31.  3. Use the component as you would use a regular TTimer. The only difference is
  32.     the new Snooze property, that can be consulted/edited at run-time. For example,
  33.     the following code (OnTimer event) emits a sound whenever the application stays
  34.     idle for more than one minute:
  35.  
  36.     procedure TForm1.ETimer1Timer(Sender: TObject);
  37.     begin
  38.       with TETimer(Sender) do
  39.       if Snooze > 60000 then begin
  40.         MessageBeep(MB_OK);
  41.         Snooze := 0;
  42.       end;
  43.     end;
  44. *)
  45.  
  46. interface
  47.  
  48. uses Classes,ExtCtrls;
  49.  
  50. type
  51.   TETimer = class(TTimer)
  52.   private
  53.     function GetSnooze: LongInt;
  54.     procedure SetSnooze(const Value: LongInt);
  55.   public
  56.     constructor Create(AOwner: TComponent); override;
  57.     destructor Destroy; override;
  58.     property Snooze: LongInt read GetSnooze write SetSnooze;
  59. end;
  60.  
  61. procedure Register;
  62.  
  63. implementation
  64.  
  65. uses {$IFDEF WIN32}Windows;{$ELSE}WinProcs,WinTypes;{$ENDIF}
  66.  
  67. var
  68.   Instances: integer;
  69.   ElapsedTime: LongInt;
  70.   whKeyboard,whMouse: HHook;
  71.  
  72. {______________________________________________________________________________}
  73. procedure Register;
  74. begin
  75.   RegisterComponents('VNM',[TETimer]);
  76. end;
  77.  
  78. {______________________________________________________________________________}
  79. {$IFDEF WIN32}
  80. function MouseHookCallBack(Code: integer; Msg: WPARAM;
  81.                            MouseHook: LPARAM): LRESULT; stdcall;
  82. {$ELSE}
  83. function MouseHookCallBack(Code: integer; Msg: word;
  84.                            MouseHook: longint): longint; export;
  85. {$ENDIF}
  86. begin
  87.   if Code >= 0 then
  88.     ElapsedTime := GetTickCount;
  89.   Result := CallNextHookEx(whMouse,Code,Msg,MouseHook);
  90. end;
  91.  
  92. {______________________________________________________________________________}
  93. {$IFDEF WIN32}
  94. function KeyboardHookCallBack(Code: integer; Msg: WPARAM;
  95.                               KeyboardHook: LPARAM): LRESULT; stdcall;
  96. {$ELSE}
  97. function KeyboardHookCallBack(Code: integer; Msg: word;
  98.                               KeyboardHook: longint): longint; export;
  99. {$ENDIF}
  100. begin
  101.   if Code >= 0 then
  102.     ElapsedTime := GetTickCount;
  103.   Result := CallNextHookEx(whKeyboard,Code,Msg,KeyboardHook);
  104. end;
  105.  
  106. {______________________________________________________________________________}
  107. constructor TETimer.Create(AOwner:TComponent);
  108.   function GetModuleHandleFromInstance: THandle;
  109.   var
  110.     s: array[0..512] of char;
  111.   begin
  112.     GetModuleFileName(hInstance,s,sizeof(s)-1);
  113.     Result := GetModuleHandle(s);
  114.   end;
  115. begin
  116.   inherited Create(AOwner);
  117.   Inc(Instances);
  118.   if Instances = 1 then begin
  119.     ElapsedTime := GetTickCount;
  120.     whMouse := SetWindowsHookEx(WH_MOUSE,MouseHookCallBack,
  121.                                 GetModuleHandleFromInstance,
  122.                                 {$IFDEF WIN32}GetCurrentThreadID{$ELSE}
  123.                                               GetCurrentTask{$ENDIF});
  124.     whKeyboard := SetWindowsHookEx(WH_KEYBOARD,KeyboardHookCallBack,
  125.                                    GetModuleHandleFromInstance,
  126.                                    {$IFDEF WIN32}GetCurrentThreadID{$ELSE}
  127.                                                  GetCurrentTask{$ENDIF});
  128.   end;
  129. end;
  130.  
  131. {______________________________________________________________________________}
  132. destructor TETimer.Destroy;
  133. begin
  134.   Dec(Instances);
  135.   if Instances = 0 then begin
  136.     UnhookWindowsHookEx(whKeyboard);
  137.     UnhookWindowsHookEx(whMouse);
  138.   end;
  139.   inherited Destroy;
  140. end;
  141.  
  142. {______________________________________________________________________________}
  143. procedure TETimer.SetSnooze(const Value: LongInt);
  144. begin
  145.   ElapsedTime := GetTickCount + Value;
  146. end;
  147.  
  148. {______________________________________________________________________________}
  149. function TETimer.GetSnooze: LongInt;
  150. begin
  151.   result := GetTickCount - ElapsedTime;
  152. end;
  153.  
  154. end.
  155.  
  156.